package com.liveinstars.post.application.controller.content.api;

import com.liveinstars.post.application.controller.content.model.LikeModel;
import com.liveinstars.post.business.model.dto.BaseCommentDTO;
import com.liveinstars.post.business.model.dto.post.BasePostDetailDTO;
import com.liveinstars.post.business.model.dto.post.BasePostMinimalDTO;
import com.liveinstars.post.business.model.entity.PostComment;
import com.liveinstars.post.business.model.entity.Report;
import com.liveinstars.post.business.model.enums.CommentStatus;
import com.liveinstars.post.business.model.enums.PostStatus;
import com.liveinstars.post.business.model.params.PostCommentParam;
import com.liveinstars.post.business.model.params.PostContentParam;
import com.liveinstars.post.business.model.params.ReportParam;
import com.liveinstars.post.business.model.vo.*;
import com.liveinstars.post.business.service.OptionService;
import com.liveinstars.post.business.service.PostCommentService;
import com.liveinstars.post.business.service.ReportService;
import com.onepiece.shipelves.common.Result;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import javax.validation.Valid;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static org.springframework.data.domain.Sort.Direction.DESC;

/**
 * Content post controller.
 *
 * @author johnniang
 * @date 2019-04-02
 */
@RestController("ApiContentReportController")
@RequestMapping("/post/content/reports")
public class ReportController {

    private final ReportService reportService;


    public ReportController(ReportService reportService,
                            OptionService optionService) {
        this.reportService = reportService;
    }



    //查询uid的全部文章，并且获得我对这些文章的状态
    @GetMapping("/user")
    @ApiOperation("Lists posts")
    public Page<PostListVO> pageByUser(@PageableDefault(sort = "createTime", direction = DESC) Pageable pageable, @RequestParam(value = "uid") Integer uid, @RequestHeader("uid") Integer userId) {
        Page<Report> postPage = reportService.pageByUser(PostStatus.DRAFT, pageable, uid);
        return reportService.convertToListVo(postPage, userId);
    }

    @GetMapping("/admin")
    @ApiOperation("Lists posts")
    public Page<PostListVO> pageByAdmin(@PageableDefault(sort = "createTime", direction = DESC) Pageable pageable,  @RequestHeader("uid") Integer userId) {
        Page<Report> postPage = reportService.pageBy(PostStatus.DRAFT, pageable);
        return reportService.convertToListVo(postPage, userId);
    }



    @PostMapping
    @ApiOperation("Creates a post")
    public Report createBy(@Valid @RequestBody ReportParam postParam,
                                 @RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave) {
        // Convert to
        Report post = postParam.convertTo();
        return reportService.createBy(post,  autoSave);
    }



    @DeleteMapping("{postId:\\d+}")
    @ApiOperation("Deletes a photo permanently")
    public void deletePermanently(@PathVariable("postId") Integer postId) {
        reportService.removeById(postId);
    }





    @GetMapping("{postId:\\d+}")
    @ApiOperation("Gets a post")
    public PostDetailVO getBy(@PathVariable("postId") Integer postId,
                              @RequestParam(value = "formatDisabled", required = false, defaultValue = "true") Boolean formatDisabled,
                              @RequestParam(value = "sourceDisabled", required = false, defaultValue = "false") Boolean sourceDisabled,
                              @RequestHeader("uid") Integer userId) {
        PostDetailVO postDetailVO = reportService.convertToDetailVoByUserId(reportService.getById(postId), userId);


        return postDetailVO;
    }


}
